package com.foredu.fiqs.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.foredu.fiqs.domain.Department;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.DepartmentActionForm;

public class DepartmentAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到部门管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("deptList");
	}

	// 跳转到部门添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("deptAdd");
	}

	// 完成到部门添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Department dept = new Department();
		DepartmentActionForm daf = (DepartmentActionForm)form;
		dept.setDeptId(daf.getDeptId());
		dept.setDeptName(daf.getDeptName());
		managementService.saveDepartment(dept);
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);
		return mapping.findForward("deptList");
	}

	// 跳转到部门修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Department dept = new Department();
		DepartmentActionForm daf = (DepartmentActionForm)form;
		dept = managementService.findDepartmentById(daf.getId());
		request.setAttribute("dept", dept);
		return mapping.findForward("deptUpdate");
	}

	// 保存部门修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Department dept = new Department();
		DepartmentActionForm daf = (DepartmentActionForm)form;
		dept.setId(daf.getId());
		dept.setDeptId(daf.getDeptId());
		dept.setDeptName(daf.getDeptName());
		managementService.updateDepartment(dept);
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);
		return mapping.findForward("deptList");
	}

	// 完成部门删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Department dept = new Department();
		DepartmentActionForm daf = (DepartmentActionForm)form;
		managementService.deleteDepartmentById(daf.getId());
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);
		return mapping.findForward("deptList");
	}
}
